package cn.com.git.workflow.dao.impl;

import cn.com.git.common.base.BaseDaoImpl;
import cn.com.git.workflow.dao.WorkFlowConfigDAO;
import cn.com.git.workflow.dao.mapper.WorkFlowConfigMapper;
import cn.com.git.workflow.dao.model.WorkFlowConfig;
import cn.com.git.workflow.dao.model.WorkFlowConfigExample;
import cn.com.git.workflow.service.query.WorkFlowConfigListQuery;
import org.apache.commons.compress.utils.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Optional;
/**
  * WorkFlowConfigDAO接口
  * @author zhangzhenfu
  * @Description
  * @date 2020/4/12
*/
@Repository
public class WorkFlowConfigDAOImpl extends BaseDaoImpl<WorkFlowConfigMapper,WorkFlowConfig,WorkFlowConfigExample,WorkFlowConfigListQuery,Integer> implements WorkFlowConfigDAO {

    @Autowired
    private WorkFlowConfigMapper mapper;

    @Override
    protected WorkFlowConfigMapper getMapper() {
        return mapper;
    }

    @Override
    public WorkFlowConfigExample analysisLimit(WorkFlowConfigListQuery query) {
        WorkFlowConfigExample example = new WorkFlowConfigExample();
        WorkFlowConfigExample.Criteria c =  example.createCriteria();
        if(query.getId()!=null){
            c.andIdEqualTo(query.getId());
        }
        if(query.getFlowType()!=null){
            c.andFlowTypeEqualTo(query.getFlowType());
        }
        if(query.getCurrentNode()!=null){
            c.andCurrentNodeEqualTo(query.getCurrentNode());
        }
        if(query.getNextNode()!=null){
            c.andNextNodeEqualTo(query.getNextNode());
        }
        return example;
    }

    @Override
    public WorkFlowConfig selectById(Integer id) {
        return super.selectByPrimaryKey(id);
    }

    @Override
    public WorkFlowConfig selectOne(WorkFlowConfigListQuery query) {
        WorkFlowConfigExample example = analysisLimit(query);
        //TODO
        List<WorkFlowConfig> list =   Optional.ofNullable(super.selectByExample(example)).orElse(Lists.newArrayList());
            if(list.size()>0){
                return list.get(0);
            }
        return null;
    }

    @Override
    public List<WorkFlowConfig> selectList(WorkFlowConfigListQuery query) {
        WorkFlowConfigExample example = analysisLimit(query);
        return Optional.ofNullable(super.selectByExample(example)).orElse(Lists.newArrayList());
    }

    @Override
    public int countLimit(WorkFlowConfigListQuery query) {
        WorkFlowConfigExample example = analysisLimit(query);
        return countByExample(example);
    }

    @Override
    public Integer insertOne(WorkFlowConfig record) {
        super.insertSelective(record);
        return record.getId();
    }

    @Override
    public List<WorkFlowConfig> insertList(List<WorkFlowConfig> list) {
        Optional.ofNullable(list).orElse(Lists.newArrayList()).forEach(s->{
            super.insertSelective(s);
        });
        return list;
    }

    @Override
    public Integer updateOne(WorkFlowConfig record) {
        return super.updateByPrimaryKeySelective(record);
    }

    @Override
    public Integer updateLimit(WorkFlowConfig record, WorkFlowConfigListQuery query) {
        WorkFlowConfigExample example = analysisLimit(query);
        return super.updateByExampleSelective(record,example);
    }

    @Override
    public Integer deleteOne(Integer id) {
        return super.deleteByPrimaryKey(id);
    }

    @Override
    public Integer deleteLimit(WorkFlowConfigListQuery query) {
        WorkFlowConfigExample example = analysisLimit(query);
        return super.deleteByExample(example);
    }

}